<?php
include '../connect.php';
include '../_mysql.php';
include '../_settings.php';
include '../_functions.php';

$username = $_POST['username'];
$email = $_POST['email'];
$country = $_POST['country'];
$sex = $_POST['sex'];
$pass = md5(stripslashes($_POST['pwd']));
$step = $_POST['step'];

$is_ajax = $_REQUEST['is_ajax'];
$json_result = array();

if(isset($is_ajax) && $is_ajax)
{
	
	//Check user exists
	$result = safe_query("SELECT * FROM ".PREFIX."user WHERE username = '".$username."' LIMIT 0 , 1");
	if (!$result) {
		$json_result["success"] = false;
		$json_result["message"] = "Can't request to server.";
	} else {
		if ($step == "step-1") {
			if (mysql_fetch_row($result)) {
				$json_result["success"] = false;
				$json_result["message"] = "The username already exist in our system";
			} else {
				$json_result["success"] = true;
				$json_result["message"] = "You may regiter with this username";
			}
		} else {
			$result2 = safe_query("SELECT * FROM ".PREFIX."user WHERE email = '".$email."' LIMIT 0 , 1");
			if (!mysql_fetch_row($result2)) {
				safe_query("INSERT INTO ".PREFIX."user (username, password, money, email) VALUES ('".$username."', '".$pass."', 0, '".$email."')");
				$json_result["success"] = true;
				$json_result["message"] = "Welcome! You may now login.";
			} else {
				$json_result["success"] = false;
				$json_result["message"] = "The email already exist in our system";
			}
		}
	}
}
else
{
	redirect('index.php','',0);
}

echo json_encode($json_result);

?>